
package com.wwy.monitor.service.impl;

import com.wwy.monitor.common.MessageException;
import com.wwy.monitor.common.PageResult;
import com.wwy.monitor.mapper.tddl.MonitorConfigMapper;
import com.wwy.monitor.model.tddl.dto.MonitorConfigDTO;
import com.wwy.monitor.model.tddl.query.MonitorConfigQuery;
import com.wwy.monitor.service.MonitorConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author tianquan.ld
 * @version V1.0
 * <p>Description: çæ§éç½®service impl</p>
 * @date 2019-10-29 21:15
 */
@Service(value = "monitorConfigService")
public class MonitorConfigServiceImpl implements MonitorConfigService {
    @Autowired
    private MonitorConfigMapper monitorConfigMapper;

    @Override
    public List<MonitorConfigDTO> getAllConfig() {
        return monitorConfigMapper.selectAllConfig();
    }

    @Override
    public int addMonitorConfig(MonitorConfigDTO dto) {
        return monitorConfigMapper.addMonitorConfig(dto);
    }

    @Override
    public PageResult queryMonitorConfig(MonitorConfigQuery query) {
        verifyParam(query);
//å¯ç¨åé¡µ
//å¼å¯åé¡µ
        int pageNum = query.getPageNum();
        int pageSize = query.getPageSize();
        Integer offset = (pageNum - 1) * pageSize;
        query.setPageNum(offset);
        query.setPageSize(pageSize);
        int dataCount = monitorConfigMapper.selectCountByQuery(query);
        int pages = (dataCount + pageSize - 1) / pageSize;
        List<MonitorConfigDTO> data = monitorConfigMapper.selectByQuery(query);
//å°è£ç»æé
        PageResult result = new PageResult();
        result.setPageSize(pageSize);
        result.setCurrentPage(pageNum);
        result.setPages(pages);
        result.setTotalCount(dataCount);
        result.setDatas(data);
        return result;
    }

    @Override
    public int deleteMonitorConfig(Long id, String empId) {
        MonitorConfigDTO dto = new MonitorConfigDTO();
        dto.setId(id);
        dto.setUpdateUser(empId);
        dto.setUpdateTime(new Date());
        return monitorConfigMapper.deleteMonitorConfig(dto);
    }

    private void verifyParam(MonitorConfigQuery query) {
        if (null == query.getPageNum() || null == query.getPageSize()) {
            throw new MessageException("分页参数不能为空");
        }
        if (StringUtils.isBlank(query.getOrderBy())) {
            query.setOrderBy(" update_time desc ");
        }
        getIntListStatus(query);
        if (null != query.getStartTime()) {
            query.setStartDate(new Date(query.getStartTime()));
        }
        if (null != query.getEndTime()) {
            query.setEndDate(new Date(query.getEndTime()));
        }
    }

    private void getIntListStatus(MonitorConfigQuery query) {
        if (StringUtils.isBlank(query.getTypeStr())) {
            return;
        }
        String[] strings = query.getTypeStr().split(",");
        List<Integer> list = Lists.newArrayList();
        try {
            for (String str : strings) {
                list.add(Integer.parseInt(str));
            }
            query.setTypeInList(list);
        } catch (Exception e) {
            throw new MessageException("éæ³ç¶æ");
        }
    }
}